package cn.tedu._04weibo.controller;

import cn.tedu._04weibo.base.response.JsonResult;
import cn.tedu._04weibo.mapper.WeiboMapper;
import cn.tedu._04weibo.pojo.dto.WeiboAddParam;
import cn.tedu._04weibo.pojo.entity.Weibo;
import cn.tedu._04weibo.pojo.vo.UserVO;
import cn.tedu._04weibo.pojo.vo.WeiboDetailVO;
import cn.tedu._04weibo.pojo.vo.WeiboIndexVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;

import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@Validated
@RestController
@RequestMapping("/v1/weibo/")
@Tag(name = "02-微博模块")

public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    /**
     * 1.发布微博功能
     */
    @PostMapping("insert")
    @Operation(summary = "发布微博功能")
    @ApiOperationSupport(order = 10)
    public JsonResult insert(@RequestBody WeiboAddParam weiboAddParam, HttpSession session){
        log.debug("weiboAddParam = " + weiboAddParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
        //存入数据
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboAddParam, weibo);
        weibo.setCreateTime(new Date());
        weibo.setUserId(userVO.getId());
        weiboMapper.insertWeibo(weibo);

        return JsonResult.ok();
    }

    /**
     * 2.微博首页列表
     */
    @GetMapping("selectIndex")
    @Operation(summary = "微博首页列表")
    @ApiOperationSupport(order = 20)
    public JsonResult selectIndex(){
        List<WeiboIndexVO> weiboIndexVOS = weiboMapper.selectIndex();

        return JsonResult.ok(weiboIndexVOS);
    }

    /**
     * 3.微博详情功能
     */
    @GetMapping("selectById")
    @Operation(summary = "微博详情功能")
    @ApiOperationSupport(order = 30)
    public JsonResult selectById(@Range(min = 100, max = 500, message = "微博ID必须在100-500之间") Long id){
        log.debug("id = " + id);
        WeiboDetailVO weiboDetailVO = weiboMapper.selectById(id);

        return JsonResult.ok(weiboDetailVO);
    }
}







